/**  
* @Title: UserHouseServiceImpl.java
* @Package com.house.service.impl.user
* @version V1.0  
*/
package com.hjb.service.impl.user;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hjb.service.domain.Collect;
import com.hjb.service.domain.HouseImage;
import com.hjb.service.domain.Requirement;
import com.hjb.service.domain.Reservation;
import com.hjb.service.domain.ThouseRoom;
import com.hjb.service.mapper.CollectMapper;
import com.hjb.service.mapper.HouseImageMapper;
import com.hjb.service.mapper.RequirementMapper;
import com.hjb.service.mapper.ReservationMapper;
import com.hjb.service.mapper.ThouseRoomMapper;
import com.hjb.service.user.UserHouseService;
import com.hjb.service.utils.NumberUtil;
import com.hjb.service.utils.StringUtil;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

/**
 * @ClassName: UserHouseServiceImpl
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author <a href="mailto:125671254@qq.com">Pan Xinwen</a>
 * @date 2017年5月4日 下午4:19:07
 */
@Service
public class UserHouseServiceImpl implements UserHouseService {

	@Autowired
	private ThouseRoomMapper thouseRoomMapper;

	@Autowired
	private CollectMapper collectMapper;

	@Autowired
	private ReservationMapper reservationMapper;

	@Autowired
	private RequirementMapper requirementMapper;

	@Autowired
	private HouseImageMapper houseImageMapper;

	@Override
	public Collect getCollectHouseByHouseId(String userId, String houseId) {
		Example example = new Example(Collect.class);
		Criteria criteria = example.createCriteria();
		criteria.andEqualTo("memberId", userId);
		criteria.andEqualTo("houseId", houseId);
		List<Collect> collects = collectMapper.selectByExample(example);
		criteria = null;
		example = null;
		return collects.size() == 0 ? new Collect() : collects.get(0);
	}

	@Override
	public void collectHouse(String userId, String houseId, String sign) {
		Example example = new Example(Collect.class);
		Criteria criteria = example.createCriteria();
		criteria.andEqualTo("memberId", userId);
		criteria.andEqualTo("houseId", houseId);
		// 收藏
		if (sign.equals("0")) {
			Collect collect = new Collect();
			collect.setId(StringUtil.get22Str());
			collect.setMemberId(userId);
			collect.setHouseId(houseId);
			collect.setCollectDate(new Date());
			collect.setModTime(new Date());
			collectMapper.insertSelective(collect);
		} else {
			collectMapper.deleteByExample(example);
		}
		criteria = null;
		example = null;
	}

	@Override
	public int subscribeByHouse(String reservationTime, String mobileNo, String memberName, String userId,
			String houseId, String sign, String rentNo, String roomNo) {
		// 查询是都已经预约
		Example example = new Example(Reservation.class);
		Criteria criteria = example.createCriteria();
		criteria.andEqualTo("memberId", userId).andEqualTo("houseId", houseId).andEqualTo("rentNo", rentNo)
				.andEqualTo("roomNo", roomNo);
		List<Reservation> list = reservationMapper.selectByExample(example);

		if (list.size() > 0) {
			// Reservation reservation = new Reservation();
			// reservation.set
			Reservation reservation = new Reservation();
			reservation.setStatus(Integer.parseInt(sign));
			return reservationMapper.updateByExampleSelective(reservation, example);
		}

		Reservation reservation = new Reservation();
		reservation.setId(StringUtil.get22Str());
		reservation.setHouseId(houseId);
		reservation.setMemberId(userId);
		reservation.setStatus(Integer.parseInt(sign));
		reservation.setModTime(new Date());
		reservation.setMobileNo(mobileNo);
		reservation.setMemberName(memberName);
		reservation.setRentNo(rentNo);
		reservation.setRoomNo(roomNo);
		reservation.setReservationTime(StringUtil.formatStrToDate(reservationTime));
		return reservationMapper.insertSelective(reservation);
	}

	@Override
	public Reservation isSubscribeByHouseId(String houseId, String userId) {
		Example example = new Example(Reservation.class);
		Criteria criteria = example.createCriteria();
		criteria.andEqualTo("memberId", userId);
		criteria.andEqualTo("rentNo", houseId);
		criteria.andEqualTo("status", 2);
		List<Reservation> reservations = reservationMapper.selectByExample(example);

		criteria = null;
		example = null;
		return reservations.size() == 0 ? new Reservation() : reservations.get(0);
	}

	@Override
	public List<Map<String, Object>> getCollectHouse(String userId) {

		// Example example = new Example(Collect.class);
		// Criteria criteria = example.createCriteria();
		// criteria.andEqualTo("memberId", userId);
		// example.orderBy("collect_date").desc();
		// List<Collect> collects = collectMapper.selectByExample(example);
		// criteria=null;
		// example=null;
		return collectMapper.getCollectHouse(userId);
	}

	@Override
	public List<Map<String, Object>> getUserReservation(String userId) {

		return reservationMapper.getReservationHouses(userId);

	}

	@Override
	public int abolishSub(String id, String sign) {
		Reservation reservation = new Reservation();
		reservation.setId(id);
		reservation.setStatus(Integer.parseInt(sign));
		return reservationMapper.updateByPrimaryKeySelective(reservation);
	}

	@Override
	public int searchHouse(HttpServletRequest request) {
		Requirement requirement = new Requirement();
		requirement.setId(StringUtil.get22Str());
		requirement.setAddTime(new Date());
		requirement.setAmount(
				Integer.parseInt(request.getParameter("price") == null ? "0" : request.getParameter("price")));
		requirement.setArea(request.getParameter("area"));
		requirement.setContactName(request.getParameter("name"));
		requirement.setDescription(request.getParameter("explain"));
		requirement.setMemberId(request.getSession().getAttribute("userId") + "");
		requirement.setMobileNo(request.getParameter("mobile"));
		requirement.setStatus(1);
		requirement.setModTime(new Date());
		return requirementMapper.insertSelective(requirement);
	}

	@Override
	public List<Map<String, Object>> searchYhxhd(HttpServletRequest request) {

		int showCount = NumberUtil.getInteger(request.getParameter("pageSize"), 3);
		int currentPage = NumberUtil.getInteger(request.getParameter("pageNum"), 1);
		Page page = PageHelper.startPage(currentPage, showCount, true);

		String rentType = request.getParameter("rentType");
		String roomType = request.getParameter("roomType");
		String hosueAmount = request.getParameter("hosueAmount");
		String rentAmount = request.getParameter("rentAmount");
		String rentGenre = request.getParameter("rentGenre");
		String roomId = request.getParameter("roomId");

		Example example = new Example(ThouseRoom.class);
		example.orderBy("room_mod_time").desc();
		if (rentType != null && rentType.equals("1")) {
			example.createCriteria().andEqualTo("rentType", rentType).andEqualTo("rentStatus", "1")
					.andNotEqualTo("roomid", roomId).andEqualTo("rentGenre", rentGenre).andBetween("amount",
							Integer.parseInt(hosueAmount) * 100 - 50000, Integer.parseInt(hosueAmount) * 100 + 50000);

		} else {
			example.createCriteria().andEqualTo("roomType", roomType).andEqualTo("rentStatus", "1")
					.andNotEqualTo("roomid", roomId).andBetween("rentAmount",
							Integer.parseInt(rentAmount) - 50000, Integer.parseInt(rentAmount) + 50000);
		}
		List<ThouseRoom> houseList = thouseRoomMapper.selectByExample(example);

		List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();

		for (ThouseRoom house : houseList) {
			Map<String, Object> hmap = new HashMap<String, Object>();
			example = new Example(HouseImage.class);
			Criteria criteria1 = example.createCriteria();
			criteria1.andEqualTo("houseId", house.getId());
			example.orderBy("mod_time").desc();
			List<HouseImage> houseImage = houseImageMapper.selectByExample(example);
			hmap.put("house", house);
			hmap.put("houseImage", houseImage);
			result.add(hmap);
		}

		return result;
	}
}
